extends Node
class_name StateMachine

export var initial_state = NodePath()
onready var state = get_node(initial_state)
onready var current_state = state.name

var states = {}

func _init():
	add_to_group('state_machine')


func _ready():
	# 等待父节点ready方法执行完成
	yield(owner, "ready")
	state.enter()


func _unhandled_input(event):
	state.unhandled_input(event)


func _process(delta):
	state.process(delta)


func _physics_process(delta):
	state.physics_process(delta)


func transition_to(target_state, msg = {}):
	print(current_state, '->', target_state)
	
	if !states.has(target_state):
		print("there's no state:", target_state)
		return
	
	state.exit()
	
	state = states[target_state]
	current_state = target_state
	state.enter(msg)
